//
//  LWXNetWorkRequestManager.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 Liuwx. All rights reserved.
//

#import "LWXNetWorkRequestManager.h"

@interface LWXNetWorkRequestManager()
//存储回调 block 的属性 用 copy 会被换掉
//@property (nonatomic, copy) ResultBlock block;

@end



@implementation LWXNetWorkRequestManager

//单例方法
+ (instancetype)defaultManager {
    static LWXNetWorkRequestManager *manager = nil;
    @synchronized(self) {
        if (!manager) {
            manager = [[LWXNetWorkRequestManager alloc] init];
        }
    }
    return manager;
}

// 请求方法
+ (void)requestURL:(NSURL *)url Method: (URLRequestMethod)method Params:(NSDictionary *)param Result:(ResultBlock)resultBlock{
    //空的可变字符串
    NSMutableString *urlString = [NSMutableString new];
    if ([url isKindOfClass:[NSURL class]]) {
        //拼接 url                 转换成 string
        [urlString appendString:url.absoluteString];
  
    }else if ([url isKindOfClass:[NSString class]]) {
        
        [urlString appendString:(NSString *)url];
        
    }
    if (0 == urlString.length) {
        //                                出错的 域名        出错的编码
        NSError *error = [NSError errorWithDomain:@"EPA" code:251 userInfo:@{@"reason":@"你输入的网址不正确"}];
        resultBlock(nil,nil,error);
        //返回错误之后直接返回
        return;
    }
    
    //把参数字典转成字符串
    //创建对象分开不同的请求  manager 的声明周期 释放后 block 就为 nil 了下面的代码不能执行，所以写成单例
    LWXNetWorkRequestManager *manager = [LWXNetWorkRequestManager new];
    
    //参数字符串
    NSString *paraStr = [manager strFromDict:param];
    switch (method) {
        case LWX_GET:
        {
            if (paraStr.length > 0) {
                [urlString appendFormat:@"?%@",paraStr];
            }
            [urlString appendString:paraStr];
            [manager requestURL:[NSURL URLWithString:urlString] ResultBlock:resultBlock];
            break;
        }
            
        case LWX_POST:
        
            [manager postURL:urlString PataStr:paraStr ResultBlock:resultBlock];
            break;
            
        default:
            break;
    }
    
    
}

// GET 网络请求
- (void)requestURL:(NSURL *)url ResultBlock:(ResultBlock)block {
    

    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:block];
    //开始网络请求
    [dataTask resume];
    
}

// POST 的网路请求

- (void)postURL:(NSString *)urlStr PataStr:(NSString *)paraStr ResultBlock:(ResultBlock)block {
    
    //创建 url
    NSURL *url = [NSURL URLWithString:urlStr];
    //创建可变请求
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    //设置 POST
    [mRequest setHTTPMethod:@"POST"];
    
    //请求体
    [mRequest setHTTPBody:[paraStr dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:mRequest completionHandler:block];
    
    [dataTask resume];
    
}

- (NSString *)strFromDict:(NSDictionary *)dict {

    //定义一个空数组
    NSMutableArray *array = [NSMutableArray new];
    
    for (NSString *key in dict) {
        NSString *str = [NSString stringWithFormat:@"%@=%@",key,dict[key]];
        [array addObject:str];
    }
    //数组拼接成字符创
    NSString *para = [array componentsJoinedByString:@"&"];
    
    NSLog(@"%@",para);
    
    //返回
    return para;
    
}
@end
